﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows;

namespace iTFF.WPF.ViewModel
{
    class ColorPickerViewModel : WorkspaceViewModel
    {

        #region Fields

        Color _initialColor;
        Color _selectedColor;
        Color _oldColor;
        RelayCommand _OKCommand;
        RelayCommand _cancelCommand;

        #endregion

        #region Contructor

        public ColorPickerViewModel()
        {

        }

        public ColorPickerViewModel(Color initialColor)
        {
            _initialColor = initialColor;
        }

        #endregion

        #region Event

        public event RoutedPropertyChangedEventHandler<Color> ColorSelectionChanged;

        public void OnColorSelectionChanged()
        {
            if (ColorSelectionChanged != null)
                ColorSelectionChanged(this,  new RoutedPropertyChangedEventArgs<Color>(_oldColor, _selectedColor));
        }

        #endregion

        #region Properties

        public Color SelectedColor
        {
            get { return _selectedColor; }
            set
            {
                if (_selectedColor != value)
                {
                    _oldColor = _selectedColor;
                    _selectedColor = value;
                    OnPropertyChanged("SelectedColor");
                }
            }
        }
        
        #endregion

        #region Commands

        /// <summary>
        /// Closes the form
        /// </summary>
        public ICommand OKCommand
        {
            get
            {
                if (_OKCommand == null)
                    _OKCommand = new RelayCommand(param => OnOKCommand(param), param => CanExecuteOKCommand(param));
                return _OKCommand;
            }
        }

        /// <summary>
        /// Closes the form
        /// </summary>
        /// <param name="param"></param>
        private void OnOKCommand(object param)
        {
            if (this.CloseCommand.CanExecute(null))
                this.CloseCommand.Execute(null);
        }

        /// <summary>
        /// Can validate only if the color has changed
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        private bool CanExecuteOKCommand(object param)
        {
            return _selectedColor != null && _selectedColor != _initialColor;
        }

        /// <summary>
        /// Restores initial value then closes the form
        /// </summary>
        /// <param name="param"></param>
        public ICommand CanceCommand
        {
            get
            {
                if (_cancelCommand == null)
                    _cancelCommand = new RelayCommand(param => OnCanceCommand(param));
                return _cancelCommand;
            }
        }

        /// <summary>
        /// Restores initial value then closes the form
        /// </summary>
        /// <param name="param"></param>
        private void OnCanceCommand(object param)
        {
            SelectedColor = _initialColor;
            if (this.CloseCommand.CanExecute(null))
                this.CloseCommand.Execute(null);
        }

        #endregion

    }
}
